<template>
    <el-card class="card">
        <template #header>
            <div class="card-header">
                <span>个人中心</span>
            </div>
        </template>
        <div class="card-body">
            <el-menu default-active="0" class="el-menu-vertical-demo" @select="menuSelect">
                <el-menu-item index="0">
                    <el-icon>
                        <user />
                    </el-icon>
                    <span>我的信息</span>
                </el-menu-item>
                <el-menu-item index="1">
                    <el-icon>
                        <lock />
                    </el-icon>
                    <span>修改密码</span>
                </el-menu-item><el-menu-item index="2">
                    <el-icon>
                        <setting />
                    </el-icon>
                    <span>我的头像</span>
                </el-menu-item>
            </el-menu>
            <div class="card-info">
                <div class="info" v-if="activeIndex == 0">
                    <el-form :model="data" label-width="auto" style="max-width: 300px;margin-left:-480px">
                        <el-form-item label="昵称:"> <el-input v-model="data.name" /></el-form-item>
                        <el-form-item v-if="userNameInput" label="用户名:"> <el-input v-model="data.userName" /><span
                                style="color: #aaaaaa;font-size: 12px;">只能设置一次</span></el-form-item>
                        <el-form-item v-else label="用户名:">{{ data.userName }}</el-form-item>
                        <el-form-item label="地区:"><el-input v-model="data.region" /></el-form-item>
                        <el-form-item label="我的签名:"><el-input type="textarea" show-word-limit maxlength="60"
                                v-model="data.description" /></el-form-item>
                    </el-form>
                    <div style="margin: 60px auto;width: 100px;">
                        <el-button type="primary" @click="updateInfo">保存</el-button>
                    </div>
                </div>
                <div v-else-if="activeIndex == 1">
                    <h3>{{ title }}</h3>
                    <div class="validate" v-if="selectShow" @click="validateChange('email')">
                        <el-text style="font-size: 16px;color:#000;"><el-icon>
                                <Message />
                            </el-icon>&nbsp;邮箱验证</el-text>
                    </div>
                    <div v-if="validateShow" class="verify">
                        <div class="common-flow__info">
                            <p>为了您的账号安全，需要验证您的邮箱。验证码将通过邮件形式发送至：<strong>{{ data.email }}</strong></p>
                            <div class="code-input">
                                <el-input maxlength="6" v-model="codeValue" class="input-top" @input="inputChange" />
                                <div class="code-list">
                                    <div class="show-div">{{ codeValue[0] }}</div>
                                    <div class="show-div">{{ codeValue[1] }}</div>
                                    <div class="show-div">{{ codeValue[2] }}</div>
                                    <div class="show-div">{{ codeValue[3] }}</div>
                                    <div class="show-div">{{ codeValue[4] }}</div>
                                    <div class="show-div">{{ codeValue[5] }}</div>
                                </div>
                            </div>
                            <span class="get-code" @click="sendEmail">获取验证码</span>
                            <br>
                            <div style="margin: 60px auto;width: 300px;">
                                <el-button style="width: 300px" type="primary" :disabled="buttonDisabled"
                                    @click="checkEmail">下一步</el-button>
                            </div>

                        </div>
                    </div>
                    <div class="verify" v-if="validateTrue">
                        <div class="common-flow__info">
                            <p>密码可由数字、大写英文字母、小写英文字母、特殊字符组成，至少包含两种类型，长度要求8-16位字符。</p>
                            <br>
                            <el-input type="password" v-model="pwd.password" maxlength="16" minlength="8" size="large"
                                placeholder="请输入新密码"></el-input>
                            <p style="height: 20px;"> </p>
                            <el-input type="password" v-model="pwd.password1" maxlength="16" minlength="8" size="large"
                                placeholder="再次输入新密码"></el-input>

                        </div>
                        <div style="margin: 60px auto;width: 300px;">
                            <el-button type="primary" style="width: 300px"
                                :disabled="pwd.password == '' || pwd.password1 == ''"
                                @click="passwordChange">修改密码</el-button>
                        </div>
                    </div>



                </div>
                <div v-else-if="activeIndex == 2">
                    <div class="avatar-box">
                        <el-upload class="avatar-uploader" :action="api.upload" :show-file-list="false"
                            :on-success="handleAvatarSuccess">
                            <img v-if="avatar" :size="180" :src="avatar" class="avatar" />

                        </el-upload>
                    </div>
                </div>

            </div>
        </div>
    </el-card>
</template>

<script setup>
import { Message } from '@element-plus/icons-vue'
import { ref } from 'vue'
import api from './api'
import { ElMessage } from 'element-plus';
const data = ref({ userName: null })
const activeIndex = ref(0)
const validateShow = ref(false)
const validateType = ref('email')
const selectShow = ref(true)
const title = ref('身份验证')
const codeValue = ref('')
const buttonDisabled = ref(true)
const validateTrue = ref(false)
const avatar = ref('')
const pwd = ref({
    password: '',
    password1: ''
})
const userNameInput = ref(false)


const handleAvatarSuccess = (res) => {
    if (res.success) {
        api.updateAvatar(res.data).then(r => {
            var t = 'error'
            if (r.success) {
                t = 'success'
                avatar.value = api.show + res.data
            }
            ElMessage({ type: t, message: r.message })
        }
        )
    }
}


const inputChange = (value) => {
    if (value.length == 6) {
        buttonDisabled.value = false
    }
}

const passwordChange = () => {
    if (pwd.value.password != pwd.value.password1) {
        ElMessage({
            type: 'error',
            message: '两次密码不匹配!'
        })
        return
    }
    api.passwordChangeByEmailAndCode({
        code: codeValue.value,
        password: pwd.value.password
    }).then(res => {
        var type = 'error'
        if (res.success) {
            type = 'success'
            validateTrue.value = false
            validateShow.value = false
            buttonDisabled.value = true
            title.value = '身份验证'
            selectShow.value = true
            codeValue.value = ''
        }
        ElMessage({ type: type, message: res.message })
    })

}

const checkEmail = () => {
    api.checkCodeByEmail(codeValue.value).then(res => {
        if (res.success) {
            validateTrue.value = true
            validateShow.value = false
            title.value = '新密码设置'
        } else {
            ElMessage({
                type: 'error',
                message: "验证码错误!"
            })
        }
    })

}

const sendEmail = () => {
    api.sendEmail().then(res => {
        let type = 'error'
        if (res.success) {
            type = 'success'
        }
        ElMessage({ type: type, message: res.message })
    })
}

const validateChange = (value) => {
    validateType.value = value
    validateShow.value = true
    selectShow.value = false
    title.value = '邮箱验证'
}
const getMyself = () => {
    api.getMyself().then(res => {
        if (res.success) {
            data.value = res.data
            data.value.email = maskEmail(data.value.email)
            avatar.value = api.show + data.value.avatar
            if (data.value.userName == null) {
                userNameInput.value = true
            }
        }
    })
}
getMyself()
const updateInfo = () => {
    api.updateUser(data.value).then(res => {
        var t = 'error'
        if (res.success) {
            t = 'success'
            userNameInput.value = false
        }
        ElMessage({
            type: t,
            message: res.message
        })
    })
}

const menuSelect = (value) => {
    activeIndex.value = value
}

const maskEmail = (email) => {
    const atIndex = email.indexOf("@"); // 找到 "@" 符号的索引位置
    const username = email.slice(0, atIndex); // 获取 "@" 前面的字符串
    const maskedUsername = username.slice(0, 3) + "*".repeat(username.length - 3); // 将除前三位外的字符替换为星号
    const domain = email.slice(atIndex); // 获取 "@" 后面的字符串
    return maskedUsername + domain; // 返回替换后的电子邮件地址
}


</script>


<style lang="scss">
.card {
    max-width: 90%;
    margin: 20px auto;
    height: 700px;

    .card-body {
        display: flex;
        height: 600px;
        width: 100%;


        .el-menu-vertical-demo {
            height: 100%;
        }

        .card-info {
            padding: 20px;
            margin: 20px auto;

            h3 {
                width: 120px;
                margin: auto;
            }

            .validate {
                border: #f1f2f3 1px solid;
                width: 352px;
                height: 26px;
                border-radius: 10px;
                padding: 29px 24px;
                margin: 40px auto;
            }

            .validate:hover {
                cursor: pointer;
            }

            .verify {
                width: 404px;
                padding: 20px 0 120px;
                margin: 0 auto;
            }

            .common-flow__info {
                margin-top: 12px;
                color: #61666d;
                font-size: 12px;
                line-height: 21px;
                font-weight: 400;
            }

            .avatar-box {
                width: 180px;
                margin: auto;
            }

            .get-code {
                color: #00aeec;
                font-size: 12px;
            }

            .get-code:hover {
                cursor: pointer;
            }

            .code-button {
                margin: 30px auto;
                width: 80px;
            }

            .code-input {
                margin: 30px;
                position: relative;
                width: 340px;
                overflow: hidden;

                .input-top {
                    position: absolute;
                    top: 0;
                    right: 0;
                    width: 400px;
                    height: 40px;
                    opacity: 0;
                }


                .code-list {
                    width: 340px;
                    display: flex;
                    justify-content: space-between;
                }

                .show-div {
                    transition: all .3s;
                    border: 1px solid #dcdfe6;
                    border-radius: 6px;
                    width: 50px;
                    height: 40px;
                    font-size: 22px;
                    line-height: 40px;
                    text-align: center;
                }



            }

        }
    }
}
</style>

<style scoped>
.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>